<!--
 * @Descripttion: 库存查询列表
 * @version:
 * @Author: cxguo
 * @Date: 2019-08-02 15:03:05
 * @LastEditors: cxguo
 * @LastEditTime: 2020-11-11 20:29:51
 -->
<template>
  <div class="app-container">
    <div>
      <el-card shadow="never" class="oper-btn-card">
        <div class="clearfix">
          <div class="fl">
            <el-button type="primary" @click="btnAddData">新增转账</el-button>
          </div>
          <div class="fr">
            <el-checkbox
              v-model="searchData.isCancle"
              true-label="0"
              false-label="1"
              border
              @change="onCheckBoxChange"
            >不显示作废记录</el-checkbox>
            <el-input
              v-model="searchData.keywords"
              clearable
              style="width: 300px"
              placeholder="搜索账户名称，备注..."
            >
              <el-button slot="append" icon="el-icon-search" type="primary" :loading="btnLoading.search" @click="btnSearch()" />
            </el-input>
          </div>
        </div>
      </el-card>
      <vxe-grid
        ref="table"
        class="grid"
        border
        resizable
        highlight-hover-row
        :row-class-name="getRowClassName"
        :loading="tableLoading.table"
        :edit-config="{trigger: 'click', mode: 'row'}"
        :columns="columnsData"
        :data="tableData.table"
      />
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="pageInfo.rows"
        :page-sizes="[20,30,40]"
        :current-page.sync="pageInfo.page"
        :total="pageInfo.total"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
      />
    </div>
    <el-dialog
      title="新增转账"
      width="740px"
      height="90%"
      top="20px"
      :visible.sync="isShowDetail"
      destroy-on-close
      append-to-body
      @closed="onModalClose"
    >
      <Detail v-if="isShowDetail" ref="detail" :data="currentRow" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShowDetail = false">取 消</el-button>
        <el-button type="primary" :loading="btnLoading.save" @click="btnSaveData">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { oftenuse } from '@/libs/mixins'
import { listData as getListData, cancleBill, addData } from '@/api/finance/account-trans.js'
import Affix from '@/components/Affix'
import { getDelButton } from '@/components/Table/oper-btns.js'
import Tab from '@/views/components/Tab'
import Detail from './detail'
import DetailView from './detail-view'

export default {
  name: 'AccountTransList',
  filters: {
  },
  // eslint-disable-next-line vue/no-unused-components
  components: { DetailView, Detail, Tab, Affix },
  mixins: [oftenuse],
  props: {},
  data() {
    return {
      searchData: { isCancle: '0' },
      currentRow: null,
      isShowDetail: false,
      optionsData: { direction: [{ value: '0', label: '支出' }, { value: '1', label: '收入' }] },
      columnsData: [
        { title: '#', width: 30, type: 'expand',
          slots: {
            content: ({ row, seq }) => {
              const data = {
                props: {
                  data: row
                },
                attrs: {
                  style: 'width:1000px;'
                }
              }
              return [<DetailView {...data}/>]
            }
          }
        },
        { title: '操作', width: 60,
          slots: {
            default: (params) => {
              const { row } = params
              const { isCancle } = row
              const h = this.$createElement
              const delNode = getDelButton(h, {
                props: {},
                attrs: { title: '作废' },
                on: { click: () => { this.btnCancleData(row) } }
              })
              const divNode = h('div', {
                attrs: {
                  class: 'table-oper-btns'
                }
              }, [delNode])
              return isCancle === '1' ? [<span>已作废</span>] : [divNode]
            }
          }
        },
        { field: 'accountOut', title: '转出账户', width: 200,
          slots: {
            default: (params) => {
              const { accountNameOut } = params.row
              const value = `${accountNameOut}`
              return [<span>{value}</span>]
            }
          }
        },
        { field: 'transTimeOut', title: '转出日期', width: 160 },
        { field: 'accountIn', title: '转入账户', width: 200,
          slots: {
            default: (params) => {
              const { accountNameIn } = params.row
              const value = `${accountNameIn}`
              return [<span>{value}</span>]
            }
          }
        },
        { field: 'transTimeIn', title: '转入日期', width: 160 },
        { field: 'amount', title: '金额', width: 150 },
        { field: 'procedureAmount', title: '手续费', width: 150 },
        { field: 'procedureUser', title: '手续费支付方', width: 150,
          slots: {
            default: (params) => {
              const { procedureUser } = params.row
              const value = procedureUser === '0' ? '转出账户支付' : '转入账户支付'
              return [<span>{value}</span>]
            }
          }
        },
        { field: 'handleUserName', title: '经手人', width: 150 },
        { field: 'remarks', title: '备注', width: 200 }
      ]
    }
  },
  watch: {
  },
  created() {
    this.initData()
  },
  methods: {
    btnCancleData(row) {
      const { id } = row
      this.isAllowDel('确定要作废吗？').then(res => {
        this.cancleBill(id)
      })
    },
    btnAddData() {
      this.isShowDetail = true
    },
    btnSaveData() {
      const vm = this.$refs.detail
      const params = vm.getData()
      vm.handleVali().then(_ => {
        this.setBtnLoading('save', true)
        this.saveData(params).then(res => {
          this.setBtnLoading('save', false)
          this.getListData()
        }).catch(_ => {
          this.setBtnLoading('save', false)
        })
      })
    },
    btnUpdateData(row) {
      this.isShowDetail = true
      this.currentRow = row
    },
    btnSearch() {
      this.setBtnLoading('search', true)
      this.getListData().then(res => {
        this.setBtnLoading('search', false)
      }).catch(err => {
        console.log(err)
        this.setBtnLoading('search', false)
      })
    },
    // api
    saveData(params) {
      return new Promise((resolve, reject) => {
        addData(params).then(res => {
          resolve(res)
          if (!res.data.flag) return this.$notify.error({ title: '保存失败', message: res.data.message })
          this.$notify.success({ title: '保存成功' })
          this.isShowDetail = false
        }).catch(err => {
          reject(err)
          this.$notify.error({ title: '保存异常', message: err.message })
        })
      })
    },
    cancleBill(params) {
      return new Promise((resolve, reject) => {
        cancleBill(params).then(res => {
          if (!res.data.flag) return this.$notify.error({ title: '作废失败', message: res.data.message })
          resolve(res)
          this.$notify.success({ title: '作废成功' })
          this.getListData()
        }).catch(err => {
          reject(err)
          this.$notify.error({ title: '作废异常', message: err.message })
        })
      })
    },
    initData() {
      const params = { data: this.searchData, page: this.pageInfo }
      this.getListData(params)
    },
    getListData(params = { data: this.searchData, page: this.pageInfo }) {
      this.setTableLoading('table', true)
      return new Promise((resolve, reject) => {
        getListData(params).then(res => {
          this.setTableLoading('table', false)
          if (!res.data.flag) return
          const { records, total } = res.data.data
          this.setTableData('table', records)
          this.pageInfo.total = total
          resolve(res)
        }).catch(err => {
          this.setTableLoading('table', false)
          this.$notify.error({ title: '查询异常！', message: err.message })
          reject(err)
        })
      })
    },
    getRowClassName({ row, rowIndex, $rowIndex }) {
      if (row.isCancle === '1') {
        return 'table-row-gray'
      }
    },
    // on
    onSizeChange(val) {
      this.pageInfo.current = 1
      this.pageInfo.size = val
      this.getListData()
    },
    onCurrentChange(val) {
      this.pageInfo.current = val
      this.getListData()
    },
    onModalClose() {
      this.currentRow = null
    },
    onCheckBoxChange() {
      this.getListData()
    }
  }
}
</script>
<style lang="scss" scoped>
 .canuse-info{
     color: #636363;
     text-align: center;
     line-height: 26px;
     b { font-size: 24px;}
  }
</style>
